from django.core.urlresolvers import reverse
from models import Course
from django.utils.safestring import mark_for_escaping
def course_reverse(viewname, course=None, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None, **extrakwargs):
	#print "\n\n\nCourse class is %s" % course.__class__
	if isinstance(course,Course):
		#print "Changing course from Course to id %s (%s)" % (str(course.course_id), str(course.course_id).__class__)
		course = str(course.course_id)
	if course and course != '__default':
		if args:
			if args[0] != course: args.insert(0,course)
		elif not kwargs: kwargs = {'course':course}
		elif not 'course' in kwargs:
			kwargs['course'] = course
	if extrakwargs and not args:
		if not kwargs: kwargs = extrakwargs
		else: kwargs.update(extrakwargs)
	return reverse(viewname,urlconf,args,kwargs,prefix,current_app)
	
def remove_course(object, course_id, delete_on_empty=True):
	if not object or not course_id or not hasattr(object,'course'): return "None"
	Courses = getattr(object,'course').split(',')
	if course_id in Courses:
		Courses.remove(course_id)
	if len(Courses) == 0 and delete_on_empty:
		object.delete()
		return "Deleted"
	setattr(object,'course',','.join(Courses))
	return "Removed"
	return "None"
